using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using CommonLibrary.DBUtility;
using CommonLibrary.Model;

namespace CommonLibrary.DAL
{
    public class Linkman_Group
    {
        public bool Exsits(int LinkmanGroupNo)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@LinkmanGroupNo",SqlDbType.Int)
            };
            parameters[0].Value = LinkmanGroupNo;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "Linkman_Group_Exsits", parameters);
                if (dr.Read())
                    return true;
                else
                    return false;
            }
        }
        public bool Insert(Linkman_GroupInfo Linkman_Group)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@LinkmanGroupName",SqlDbType.VarChar,50)
            };
            parameters[0].Value = Linkman_Group.LinkmanGroupName;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "Linkman_Group_Insert", parameters);
                return rowAffected > 0;
            }
        }
        public bool Update(Linkman_GroupInfo Linkman_Group)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@LinkmanGroupNo",SqlDbType.Int),
                new SqlParameter("@LinkmanGroupName",SqlDbType.VarChar,50)
            };
            parameters[0].Value = Linkman_Group.LinkmanGroupNo;
            parameters[1].Value = Linkman_Group.LinkmanGroupName;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "Linkman_Group_Update", parameters);
                return rowAffected > 0;
            }
        }
        public bool Delete(int LinkmanGroupNo)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@LinkmanGroupNo",SqlDbType.Int)
            };
            parameters[0].Value = LinkmanGroupNo;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "Linkman_Group_Delete", parameters);
                return rowAffected > 0;
            }
        }
        public Linkman_GroupInfo GetLinkman_Group(int LinkmanGroupNo)
        {
            Linkman_GroupInfo Linkman_Group = null;
            SqlParameter[] parameters = {
                new SqlParameter("@LinkmanGroupNo",SqlDbType.Int)
            };
            parameters[0].Value = LinkmanGroupNo;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "Linkman_Group_GetDetail", parameters);
                if (dr.Read())
                {
                    Linkman_Group = new Linkman_GroupInfo();
                    Linkman_Group.LinkmanGroupNo = dr.GetInt32(0);
                    Linkman_Group.LinkmanGroupName = dr.GetString(1);
                }
                return Linkman_Group;
            }
        }
        public List<Linkman_GroupInfo> GetList(string StrWhere)
        {
            List<Linkman_GroupInfo> Linkman_Groups = new List<Linkman_GroupInfo>();
            Linkman_GroupInfo Linkman_Group = null;
            SqlParameter[] parameters = {
                new SqlParameter("@StrWhere",SqlDbType.NVarChar,4000)
            };
            parameters[0].Value = StrWhere;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "Linkman_Group_GetList", parameters);
                while (dr.Read())
                {
                    Linkman_Group = new Linkman_GroupInfo();
                    Linkman_Group.LinkmanGroupNo = dr.GetInt32(0);
                    Linkman_Group.LinkmanGroupName = dr.GetString(1);
                    Linkman_Groups.Add(Linkman_Group);
                }
                return Linkman_Groups;
            }
        }

    }
}